package arch.coal.ellipse.security.dev;

import java.sql.ResultSet;
import java.util.Properties;

import org.apache.log4j.Logger;

import arch.coal.ellipse.security.tables.BasicTable;
import arch.coal.ellipse.security.utils.ElapsedTime;
import arch.coal.ellipse.security.utils.JDBCUtil;


/**
 * Class is used to obtain data (via JDBC) based on the specific 
 * information contained within a BasicTable object.
 * @author mblackford
 *
 */
public class DataRetrieve {
	
	private BasicTable table;
	private Properties props;
	static Logger logger = Logger.getLogger(DataRetrieve.class);
	
	private DataRetrieve() {
		//no using default constructor
	}

	/**
	 * Must be constructed using a BasicTable (which contains the 
	 * specific table details to be used) and a Properties files (which
	 * contains data base info such as datasource, url, logon info). 
	 * @param _table
	 * @param _props
	 */
	public DataRetrieve(BasicTable _table, Properties _props) {
		props = _props;
		table = _table;
	}
	
	/**
	 * Using JDBC a result set will be obtained from the ELLIPSE Db.
	 * @return
	 * @throws Exception
	 */
	public BasicTable getData() throws Exception {
		
		logger.debug("Attempting to access data from " + table.getTableName());
		ElapsedTime metrics = new ElapsedTime();
		JDBCUtil db_con = new JDBCUtil( props );
		ResultSet rs = db_con.getResultSet(table.getSqlSelectStmt(), false);
		
		table.processResultSet(rs);
		
		logger.debug("closing all Db stuff. \n");
		db_con.close();
		
		logger.info(" transaction took [" + metrics.elapsedTime() + "]ms " );
		return table;
	}

}
